package com.example.homework.activity;

import android.app.Activity;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.homework.R;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public class TodoEditActivity extends AppCompatActivity {
    private EditText etTitle, etDescription;
    private TextView tvReminder;
    private Calendar reminderCalendar;
    private int todoId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_todo_edit);

        etTitle = findViewById(R.id.etTitle);
        etDescription = findViewById(R.id.etDescription);
        tvReminder = findViewById(R.id.tvReminder);
        reminderCalendar = Calendar.getInstance();

        // 初始化提醒时间（默认当前时间 + 1小时）
        reminderCalendar.add(Calendar.HOUR_OF_DAY, 1);
        tvReminder.setText(formatTime(reminderCalendar.getTimeInMillis()));

        // 选择提醒时间按钮
        findViewById(R.id.btnSelectTime).setOnClickListener(v -> showTimePicker());

        // 处理传入的待办数据（编辑模式）
        if (getIntent().hasExtra("id")) {
            todoId = getIntent().getIntExtra("id", -1);
            etTitle.setText(getIntent().getStringExtra("title"));
            etDescription.setText(getIntent().getStringExtra("description"));
            reminderCalendar.setTimeInMillis(getIntent().getLongExtra("reminderTime", 0));
            tvReminder.setText(formatTime(reminderCalendar.getTimeInMillis()));
        }

        // 保存按钮点击事件
        findViewById(R.id.btnSave).setOnClickListener(v -> saveTodo());
    }

    private void showTimePicker() {
        TimePickerDialog dialog = new TimePickerDialog(this, (view, hourOfDay, minute) -> {
            reminderCalendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
            reminderCalendar.set(Calendar.MINUTE, minute);
            tvReminder.setText(formatTime(reminderCalendar.getTimeInMillis()));
        }, reminderCalendar.get(Calendar.HOUR_OF_DAY), reminderCalendar.get(Calendar.MINUTE), true);
        dialog.show();
    }

    private void saveTodo() {
        String title = etTitle.getText().toString().trim();
        String description = etDescription.getText().toString().trim();
        long reminderTime = reminderCalendar.getTimeInMillis();

        if (title.isEmpty()) {
            etTitle.setError("标题不能为空");
            return;
        }

        Intent intent = new Intent();
        intent.putExtra("id", todoId == -1 ? 0 : todoId); // 新增时 id 为 0，编辑时为原有 id
        intent.putExtra("title", title);
        intent.putExtra("description", description);
        intent.putExtra("reminderTime", reminderTime);
        setResult(Activity.RESULT_OK, intent);
        finish();
    }

    private String formatTime(long timeInMillis) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        return format.format(timeInMillis);
    }
}
